package com.example.myapplication2;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.myapplication2.POJO.Shop.Good;
import com.example.myapplication2.dataBase.ShopHelper;


import java.util.Date;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ActivityResultLauncher<Intent> intentActivityResultLauncher;

    public static HashMap<String, String> map = new HashMap<>();
    private static MainActivity mainActivity;
    public static ShopHelper shopHelper;

    public static MainActivity getInstance() {
        return mainActivity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获得分享
        //获取intent
        Intent intent4 =getIntent();
        String action = intent4.getAction();
        String type = intent4.getType();
        //设置接收类型为文本
        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                Toast.makeText(this, "得到意图", Toast.LENGTH_SHORT).show();
                String data = intent4.getStringExtra(Intent.EXTRA_TEXT);
                Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
            }
        }
        findViewById(R.id.exit).setOnClickListener(this);
        findViewById(R.id.send).setOnClickListener(this);
        findViewById(R.id.jump).setOnClickListener(this);
        findViewById(R.id.Drawable).setOnClickListener(this);
        findViewById(R.id.Drawable2).setOnClickListener(this);
        findViewById(R.id.InetAddress).setOnClickListener(this);
        findViewById(R.id.CheckBox).setOnClickListener(this);
        findViewById(R.id.switch1).setOnClickListener(this);
        findViewById(R.id.radioButton).setOnClickListener(this);
        findViewById(R.id.editText).setOnClickListener(this);
        findViewById(R.id.editText2).setOnClickListener(this);
        findViewById(R.id.editText3).setOnClickListener(this);
        findViewById(R.id.AlertDialog).setOnClickListener(this);
        findViewById(R.id.DatePickerDialog).setOnClickListener(this);
        findViewById(R.id.TimePickerDialog).setOnClickListener(this);
        findViewById(R.id.Login).setOnClickListener(this);
        findViewById(R.id.SharedPrefences).setOnClickListener(this);
        findViewById(R.id.SQLiteDatabase).setOnClickListener(this);
        findViewById(R.id.DataBaseCRUD).setOnClickListener(this);
        findViewById(R.id.FileStorage).setOnClickListener(this);
        findViewById(R.id.ImageFile).setOnClickListener(this);
        findViewById(R.id.SharedVariables).setOnClickListener(this);
        findViewById(R.id.RoomBook).setOnClickListener(this);
        findViewById(R.id.shopCar).setOnClickListener(this);
        findViewById(R.id.ShopMall).setOnClickListener(this);
        findViewById(R.id.Initialization).setOnClickListener(this);
        findViewById(R.id.client).setOnClickListener(this);
        findViewById(R.id.PermissionsLazy).setOnClickListener(this);
        findViewById(R.id.PermissionsHungry).setOnClickListener(this);
        findViewById(R.id.ContactsActivity).setOnClickListener(this);
        findViewById(R.id.SMSActivity).setOnClickListener(this);
        findViewById(R.id.MMSActivity).setOnClickListener(this);
        findViewById(R.id.MMS2Activity).setOnClickListener(this);
        findViewById(R.id.InstallAppActivity).setOnClickListener(this);
        findViewById(R.id.Spinner).setOnClickListener(this);
        findViewById(R.id.Spinner2).setOnClickListener(this);
        findViewById(R.id.BaseAdapter).setOnClickListener(this);
        findViewById(R.id.BaseAdapter2Activity).setOnClickListener(this);
        findViewById(R.id.Text_reception).setOnClickListener(this);
        findViewById(R.id.GridViewActivity).setOnClickListener(this);
        findViewById(R.id.ViewPagerActivity).setOnClickListener(this);
        findViewById(R.id.PagerTabStripActivity).setOnClickListener(this);
        findViewById(R.id.Pager2Activity).setOnClickListener(this);
        findViewById(R.id.FragmentDemoActivity).setOnClickListener(this);
        findViewById(R.id.FragmentDemo2Activity).setOnClickListener(this);
        findViewById(R.id.FragmentDemo2Activity).setOnClickListener(this);
        findViewById(R.id.FragmentDemo2Activity).setOnClickListener(this);
        findViewById(R.id.broadcast).setOnClickListener(this);
        findViewById(R.id.OrderBroadcast).setOnClickListener(this);
        findViewById(R.id.staticBroadcast).setOnClickListener(this);
        findViewById(R.id.SystemBroadcast).setOnClickListener(this);
        findViewById(R.id.ScreenRotation).setOnClickListener(this);
        findViewById(R.id.DefaultPortrait).setOnClickListener(this);
        findViewById(R.id.ApplyPictureInPicture).setOnClickListener(this);
        findViewById(R.id.ServiceActivity).setOnClickListener(this);
        findViewById(R.id.RemoteServiceActivity).setOnClickListener(this);
        mainActivity = this;
        intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                Intent data = result.getData();
                if (data != null) {
                    Bundle extras = data.getExtras();
                    if (extras != null) {
                        if (extras != null) {
                            String message = extras.getString("message");
                            TextView viewById = findViewById(R.id.textView3);
                            viewById.setText(message);
                        }
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.exit:
                finish();
                break;
            case R.id.jump:
                startActivity(new Intent(this, JumpActivity.class));
                break;
            case R.id.RemoteServiceActivity:
                startActivity(new Intent(this, RemoteServiceActivity.class));
                break;
            case R.id.ServiceActivity:
                startActivity(new Intent(this, ServiceActivity.class));
                break;
            case R.id.ScreenRotation:
                startActivity(new Intent(this, ScreenRotationActivity.class));
                break;
            case R.id.ApplyPictureInPicture:
                startActivity(new Intent(this, ApplyPictureInPictureActivity.class));
                break;
            case R.id.DefaultPortrait:
                startActivity(new Intent(this, DefaultPortraitActivity.class));
                break;
            case R.id.broadcast:
                startActivity(new Intent(this, BroadcastStandardActivity.class));
                break;
            case R.id.SystemBroadcast:
                startActivity(new Intent(this, SystemBroadcastActivity.class));
                break;
            case R.id.OrderBroadcast:
                startActivity(new Intent(this, OrderBroadcastStandardActivity.class));
                break;
            case R.id.staticBroadcast:
                startActivity(new Intent(this, StaticBroadcastActivity.class));
                break;
            case R.id.billAddActivity:
                startActivity(new Intent(this, billAddActivity.class));
                break;
            case R.id.billActivity:
                startActivity(new Intent(this, billActivity.class));
                break;
            case R.id.FragmentDemo2Activity:
                startActivity(new Intent(this, FragmentDemo2Activity.class));
                break;
            case R.id.PagerTabStripActivity:
                startActivity(new Intent(this, PagerTabStripActivity.class));
                break;
            case R.id.Pager2Activity:
                startActivity(new Intent(this, Pager2Activity.class));
                break;
            case R.id.InetAddress:
                startActivity(new Intent(this, InetAddressActivity.class));
                break;
            case R.id.radioButton:
                startActivity(new Intent(this, RadioButtonActivity.class));
                break;
            case R.id.FragmentDemoActivity:
                startActivity(new Intent(this, FragmentDemoActivity.class));
                break;
            case R.id.CheckBox:
                startActivity(new Intent(this, CheckBoxActivity.class));
                break;
            case R.id.SharedVariables:
                startActivity(new Intent(this, SharedVariablesActivity.class));
                break;
            case R.id.Spinner:
                startActivity(new Intent(this, SpinnerActivity.class));
                break;
            case R.id.GridViewActivity:
                startActivity(new Intent(this, GridViewActivity.class));
                break;
            case R.id.Spinner2:
                startActivity(new Intent(this, Spinner2Activity.class));
                break;
            case R.id.ViewPagerActivity:
                startActivity(new Intent(this, ViewPagerActivity.class));
                break;
            case R.id.switch1:
                startActivity(new Intent(this, SwitchActivity.class));
                break;
            case R.id.BaseAdapter2Activity:
                startActivity(new Intent(this, BaseAdapter2Activity.class));
                break;
            case R.id.shopCar:
                startActivity(new Intent(this, ShopCarActivity.class));
                break;
            case R.id.ShopMall:
                startActivity(new Intent(this, ShopMallActivity.class));
                break;
            case R.id.RoomBook:
                startActivity(new Intent(this, RoomBookActivity.class));
                break;
            case R.id.InstallAppActivity:
                startActivity(new Intent(this, InstallAppActivity.class));
                break;
            case R.id.Initialization:
//                初始化
                Initialization();
                Toast.makeText(this, "操作成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.FileStorage:
                startActivity(new Intent(this, FileStorageActivity.class));
                break;
            case R.id.BaseAdapter:
                startActivity(new Intent(this, BaseAdapterActivity.class));
                break;
            case R.id.SMSActivity:
                startActivity(new Intent(this, SmsReadActivity.class));
                break;
            case R.id.ImageFile:
                startActivity(new Intent(this, ImageFileActivity.class));
                break;
            case R.id.SharedPrefences:
                startActivity(new Intent(this, SharedPrefencesActivity.class));
                break;
            case R.id.editText:
                startActivity(new Intent(this, editTextActivity.class));
                break;
            case R.id.DataBaseCRUD:
                startActivity(new Intent(this, DataBaseCRUDActivity.class));
                break;
            case R.id.PermissionsLazy:
                startActivity(new Intent(this, PermissionsLazyActivity.class));
                break;
            case R.id.PermissionsHungry:
                startActivity(new Intent(this, PermissionsHungryActivity.class));
                break;
            case R.id.ContactsActivity:
                startActivity(new Intent(this, ContactsActivity.class));
                break;
            case R.id.SQLiteDatabase:
                startActivity(new Intent(this, SQLiteDatabaseActivity.class));
                break;
            case R.id.editText2:
                startActivity(new Intent(this, editText2Activity.class));
                break;
            case R.id.editText3:
                startActivity(new Intent(this, editText3Activity.class));
                break;
            case R.id.MMSActivity:
                startActivity(new Intent(this, MmsActivity.class));
                break;
            case R.id.MMS2Activity:
                startActivity(new Intent(this, Mms2Activity.class));
                break;
            case R.id.DatePickerDialog:
                startActivity(new Intent(this, DatePickerDialogActivity.class));
                break;
            case R.id.TimePickerDialog:
                startActivity(new Intent(this, TimePickerDialogActivity.class));
                break;
            case R.id.Drawable:
                startActivity(new Intent(this, DrawableActivity.class));
                break;
            case R.id.Drawable2:
                startActivity(new Intent(this, DrawableNineActivity.class));
                break;
            case R.id.AlertDialog:
                startActivity(new Intent(this, AlertDialogActivity.class));
                break;
            case R.id.Text_reception:
                startActivity(new Intent(this, TextReception.class));
                break;
            case R.id.Login:
                startActivity(new Intent(this, LoginActivity.class));
                break;
            case R.id.client:
                startActivity(new Intent(this, clientActivity.class));
                break;
            case R.id.send:
                TextView viewById = findViewById(R.id.textView);
                CharSequence text = viewById.getText();
                String s = text.toString();
                Intent intent = new Intent(this, ReceiveActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("mess", s);
                bundle.putString("date", new Date().toString());
                intent.putExtras(bundle);
                intentActivityResultLauncher.launch(intent);
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        shopHelper = ShopHelper.getInstance(this);
        shopHelper.openLink();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        shopHelper.closeLink();
    }

    //初始化具体方式
    private boolean Initialization() {
        for (int i = 0; i < 6; i++) {
            Good good = new Good();
            good.setGoodName("商品2" + i);
            good.setImg("商品图片2" + i);
            good.setIntroduce("商品介绍2" + i);
            good.setPrice((long) (301 + i));
            shopHelper.addGood(good);
        }
        return true;
    }
}